package com.heima.wemedia.controller.v1;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewExamineDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class UserAdminExamineController {

    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @PostMapping("/list_vo")
    ResponseResult listVo(@RequestBody NewExamineDto dto){

        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        if (dto.getTitle() != null) {
            wrapper.like(WmNews::getTitle, "%" + dto.getTitle() + "%");
        }
        if (dto.getStatus() != null) {
            wrapper.eq(WmNews::getStatus, dto.getStatus());
        }

        Page result = wmNewsService.page(page, wrapper);
        ResponseResult responseResults = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        responseResults.setData(result.getRecords());
        responseResults.setCode(200);
        return responseResults;
    }


    @GetMapping("/one_vo/{id}")
    public ResponseResult oneVo(@PathVariable("id") Integer id) {
        WmNews wmNews = wmNewsService.getById(id);
        //String jsonWmNews = JSON.toJSONString(wmNews);
        ResponseResult responseResult = new ResponseResult();
        //responseResult.setData(jsonWmNews);
        responseResult.setData(wmNews);
        return responseResult;
    }


    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewExamineDto dto) {
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setReason("审核成功");
        wmNews.setStatus(Short.parseShort(dto.getStatus()+""));
        boolean flag = wmNewsService.updateById(wmNews);
        //不太好封装，先不写了
//        if (flag) {
//            WmNews byId = wmNewsService.getById(dto.getId());
//            //人工审核成功应该异步更新文章模块
//            kafkaTemplate.send(WmNewsMessageConstants.WM_NEWS_SAVE_TOPIC, JSON.toJSONString(byId));
//        }
        return ResponseResult.okResult("操作成功");
    }

    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewExamineDto dto) {
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(Short.parseShort(dto.getStatus()+""));
        wmNews.setReason(dto.getMsg());
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult("操作成功");
    }

}
